{------------------------------------------------------------------------------
#
#	Apple Macintosh Developer Technical Support
#
#	Exception handling for MPW Pascal, MacApp and MPW C
#
#	UFailure (aka Signals) - ÒExceptional code, with a few exceptions.Ó
#
#	TestCignal.p	-	Test tool for Pascal access to enhanced UFailure
#
#	Copyright © 1985-1988 Apple Computer, Inc.
#	All rights reserved.
#
#	Versions:	1.0					11/88
#
#	Components:	UFailure.p			November 1, 1988
#				UFailure.h			November 1, 1988
#				UFailure.inc1.p		November 1, 1988
#				UFailure.a			November 1, 1988
#				TestCignal.c		November 1, 1988
#				TestCignal.make		November 1, 1988
#				TestSignal.p		November 1, 1988
#				TestSignal.make		November 1, 1988
#
#	UFailure (or Signals) is a set of exception handling routines suitable for
#	use with MacApp, MPW C, and MPW Pascal. It is a jazzed-up version of the MacApp
#	UFailure unit. There is a set of C interfaces to it as well.
#
------------------------------------------------------------------------------}

Program TestSignals;

USES
	MemTypes, QuickDraw, OSIntf, ToolIntf, PackIntf,
	UFailure;
{$D+}


PROCEDURE DoCatchOutMain(s:STRING; long:LONGINT);
BEGIN
	Writeln(s, long:2);
	Exit(TestSignals);
END; {DoCatchOutMain}

		


FUNCTION Value:LONGINT;
	VAR
		code:	INTEGER;
		
	PROCEDURE Never;
		VAR
			code:	INTEGER;
			fi:		FailInfo;
	
		PROCEDURE Handler(code: INTEGER; message: LONGINT);
			BEGIN
				Writeln('Handler from Never; message = ',message:2,', code = ',code:2);
				{this will do an implicit Failure() when it exits}
			END;

		BEGIN {Never}
			CatchFailures(fi, Handler);
	
			code := CatchSignal;
			IF code <> 0 THEN BEGIN
				Writeln('Never shouldnÕt get here; code=', code:2);
				Value := code;
				Exit(Never);
			END;
		
			FreeSignal; {"free" the last CatchSignal}
			
			SignalMessage(7, 77777); {Signal a 7 to the last Catch (in this case}
		END;{Never}


	PROCEDURE Failer;
		BEGIN
			IF CatchSignal = 0 THEN
				Never;
			
			Failure(69, 0);			{fail no matter what}
		END; {Failer}
		
	BEGIN {Value}
		code := CatchSignal;
		IF code <> 0 THEN BEGIN
			Writeln('ShouldnÕt be here in Value, code=', code:2);
			Value := code;
			Exit(Value);
		END;
		
		{when this does its return the CatchSignal above will be automatically popped}
		code := CatchSignal;
		IF code <> 0 THEN BEGIN
			Value := code;
			Exit(Value);
		END;
		
		Failer;
	END;{Value}
 
PROCEDURE Main;
	
	VAR
		aString:		Str255;
		code:			INTEGER;
		registerLong:	LONGINT;

	BEGIN

		registerLong := 0;
		
		{catch Signals not otherwise caught by the program}
		code := CatchSignal;
		IF code <> 0 THEN BEGIN
			NumToString(code, aString);
			aString := Concat('Signal caught from main, code = ',aString,
				', registerLong = ');
			DoCatchOutMain(aString, registerLong);
		END;
		
		registerLong := $FFFF;
		
		Signal(Value);
	END; {Main}
	
BEGIN {PROGRAM}
	InitSignals; {Call this with other (i.e. toolbox) inits}
	Main;
END.
